package com.wxy.demo.generics;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wxy.demo.dto.UserDto;
import com.wxy.demo.entity.Person;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

/**
 * <p>
 * 泛型方法
 * </p>
 *
 * @author e-Xiaoyuan.Wang
 * @since 2022/10/14 11:04
 */
public class Demo1 {

    public static void main(String[] args) throws JsonProcessingException {
//        printArray(new Integer[]{1, 2, 3});
        var json = """
                {"name":"张三"}
                """;
        Person person = getBean(json);
        System.out.println(person);

//        Person person = printType();
    }

    public static <E> void printArray(E[] inputArray) {
        // 输出数组元素
        for (E element : inputArray) {
            System.out.printf("%s ", element);
        }
        System.out.println();
    }

    public static <T> T getBean(String json) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(json, new TypeReference<>() {
        });
    }

    public static <T> T printType(){
        TypeReference<T> typeReference = new TypeReference<>(){};
        Type type = typeReference.getClass().getGenericSuperclass();
        System.out.println(type);
        Type type1 = ((ParameterizedType) type).getActualTypeArguments()[0];
        System.out.println(type1);
        return null;
    }}
